package com.example.demo.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.example.demo.dao.BookRepository;
import com.example.demo.entity.Book;

@Controller
@RequestMapping("/book")
public class ReadingListController {
	@Resource
	private BookRepository bookRepository;
	
	@RequestMapping(value="/{reader}", method=RequestMethod.GET)
	public String readerBook(@PathVariable("reader")String reader, Model model){
		List<Book> readingList = bookRepository.findByReader(reader);
		if(null != readingList){
			model.addAttribute("books", readingList);
		}
		return "readingList";
	} 
	
	@RequestMapping(value="/{reader}", method=RequestMethod.POST)
	public String addToReadingList(@PathVariable("reader")String reader, Book book){
		if(null != book){
			book.setReader(reader);
			bookRepository.save(book);
		}
		return "redirect:/book/{reader}";
	}
}
